package eg.edu.eelu.fyp2013.jdetector.core.swt;

import java.awt.image.BufferedImage;
import java.util.ArrayList;

import javax.swing.Icon;

import org.omg.CORBA.portable.ValueBase;
import org.omg.CORBA.portable.ValueOutputStream;

import eg.edu.eelu.fyp2013.jdetector.core.input_output.AMatrix;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.FeatureExtraction;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.OutputObjectArray;
import matlabcontrol.MatlabConnectionException;
import matlabcontrol.MatlabInvocationException;
import matlabcontrol.MatlabProxy;
import matlabcontrol.MatlabProxyFactory;
import matlabcontrol.extensions.MatlabNumericArray;
import matlabcontrol.extensions.MatlabTypeConverter;
import SWT.SWT;

import com.mathworks.toolbox.javabuilder.MWException;
import com.mathworks.toolbox.javabuilder.MWNumericArray;

import eg.edu.eelu.fyp2013.jdetector.core.input_output.AMatrix;
import eg.edu.eelu.fyp2013.jdetector.core.input_output.FeatureExtraction;


public class swtFExtraction {
	
public SWT mySWT;
		
	public ArrayList<FeatureExtraction> swtExtract(int level , 
			ArrayList<AMatrix> DataPrerpocImages  ) {
	    //Create a proxy, which we will use to control MATLAB
	  
	  //  AMatrix A = new AMatrix();
	    
	 
	  //SWT.readrgb(arg0, arg1)
	    double ll = (double) level;
	    ArrayList<FeatureExtraction> Swtf = new ArrayList<FeatureExtraction>(DataPrerpocImages.size());
		
	    for(int k = 0; k < DataPrerpocImages.size(); k++)
	    {
	    	int tempPixelColor = 0 ;
		    
	    	 double [][] redmatrix = new double [DataPrerpocImages.get(k).width][DataPrerpocImages.get(k).height];
	 	    double [][] greenmatrix = new double [DataPrerpocImages.get(k).width][DataPrerpocImages.get(k).height];
	 	    double [][] bluematrix = new double [DataPrerpocImages.get(k).width][DataPrerpocImages.get(k).height];
	    
	      for(int i = 0 ; i < DataPrerpocImages.get(k).height; i++)
	      {
	    	for(int j = 0; j < DataPrerpocImages.get(k).width ; j++)
	    	{
	    		 tempPixelColor = DataPrerpocImages.get(k).GetImage.getRGB(j, i);
	    		 int red = (0xFF & tempPixelColor>>16);
	    		 redmatrix[i][j] = red;
	    		 int green = (0xFF & tempPixelColor>>8);
	    		 greenmatrix[i][j] = green;
	    		 int blue = (0xFF & tempPixelColor);
	    		 bluematrix[i][j] = blue;
	    		 
	    	}
	      }
	     
	   //   processor.setNumericArray("red", new MatlabNumericArray(redmatrix,null));
	   //   processor.setNumericArray("green", new MatlabNumericArray(greenmatrix,null));
	   //   processor.setNumericArray("blue", new MatlabNumericArray(bluematrix,null));
	//	  proxy.setVariable("level", level);
	 //     proxy.eval("[extractImageAll, extractImagechd,extractImagecvd,extractImagecdd,extractImagedecl] = readrgb(red,green,blue,level)");
	      

	      Object [] swtinputArray = new Object[4];
	      swtinputArray[0] = redmatrix;
	      swtinputArray[1] = greenmatrix;
	      swtinputArray[2] = bluematrix;
	      swtinputArray[3] = ll;
	        
	      
	      
	      FeatureExtraction F = new FeatureExtraction();
	   //   F.extractImageAll = processor.getNumericArray("extractImageAll").getRealArray2D();
	   //   F.extractImagechd = processor.getNumericArray("extractImagechd").getRealArray2D();
	   //   F.extractImagecvd = processor.getNumericArray("extractImagecvd").getRealArray2D();
	   //   F.extractImagecdd = processor.getNumericArray("extractImagecdd").getRealArray2D();
	  //    F.extractImagedecl = processor.getNumericArray("extractImagedecl").getRealArray2D();
          
//	      double [][] RetImageAll = new double [255][255];
	//      double [][] RetImagechd = new double [255][255];
	//      double [][] RetImagecvd = new double [255][255];
	//      double [][] RetImagecdd = new double [255][255];
	//      double [][] RetImagedecl= new double [255][255];
	      
	      
	      
	      MWNumericArray [] M = new MWNumericArray[5];
	    try {
			mySWT = new SWT();
			mySWT.readrgb(M, swtinputArray);
		
			
			
		} catch (MWException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	    
	    double [] All = M[0].getDoubleData();
	    double [] H = M[1].getDoubleData();
	    double [] V = M[2].getDoubleData();
	    double [] D = M[3].getDoubleData();
	    double [] ecl = M[4].getDoubleData();
	    
	    F.extractImageAll = new double [256][256];
	    F.extractImagechd = new double [256][256];
	    F.extractImagecvd = new double [256][256];
	    F.extractImagecdd = new double [256][256];
	    F.extractImagedecl = new double [256][256];
	    
	    int  countarr = 0;
	    for(int m = 0; m < 256; m++)
	    {
	    	for(int n = 0; n < 256 ; n++)
	    	{
	    		 F.extractImageAll[m][n] = All[countarr];
	    		
	    		 F.extractImagechd[m][n] = H[countarr];
	             F.extractImagecvd[m][n] = V[countarr];
	    		 F.extractImagecdd[m][n] = D[countarr];
	    		 F.extractImagedecl[m][n] = ecl[countarr];
	    		 
	    		 countarr++;
	    		
	    	}
	    }
	    countarr = 0;
	    F.label = DataPrerpocImages.get(k).label;
        F.width = DataPrerpocImages.get(k).width;
        F.height = DataPrerpocImages.get(k).height;
        
        F.extractImageAlli = ConvertMatrixtoimage(F.extractImageAll, F.width, F.height);
        F.extractImagechdi = ConvertMatrixtoimage(F.extractImagechd, F.width, F.height);
        F.extractImagecvdi = ConvertMatrixtoimage(F.extractImagecvd, F.width, F.height);
        F.extractImagecddi = ConvertMatrixtoimage(F.extractImagecdd , F.width, F.height);
        F.extractImagedecli = ConvertMatrixtoimage(F.extractImagedecl, F.width, F.height);
        
	    Swtf.add(F);
	    //Object mmm = swtoutputarray[0];
	    // MWNumericArray M = new MWNumericArray(mmm);
	    // double [] mm = M.getDouble();
	 
	    // double [][] mm = (double[][]) mmm;
	     
	    
	    
      /*    F.extractImagechd = swtoutputarray[1].OutputArrray;
	     F.extractImagecvd = swtoutputarray[2].OutputArrray;
	     F.extractImagecdd = swtoutputarray[3].OutputArrray;
	     F.extractImagedecl =swtoutputarray[4].OutputArrray;
	    */
	    
	    
	      
	    //double [][] swtvaluesmap = processor.getNumericArray("map").getRealArray2D();
	  
	    }
	    
   	    return Swtf;
	}
	
	public BufferedImage ConvertMatrixtoimage (double [][] data , int w , int h )
	{
		BufferedImage Output = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
	    
	    for(int i = 0 ; i < Output.getWidth(); i++)
	    {
	    	for(int  j = 0; j < Output.getHeight(); j++)
	    	{
	    		int rgb = (int)data[i][j]<< 16 | (int) data[i][j] << 8 | (int) data [i][j];
	    		Output.setRGB(i, j, rgb);
	    		
	    	}
	    }
		
		return Output;
 		
	}

}

